package com.pay.cloud.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.pay.cloud.KeyInfo;
import com.pay.cloud.KeyInfoListener;
import com.pay.cloud.R;

import java.util.ArrayList;
import java.util.List;

import static com.pay.cloud.KeyInfo.KEY_NUM_0;
import static com.pay.cloud.KeyInfo.KEY_NUM_1;
import static com.pay.cloud.KeyInfo.KEY_NUM_2;
import static com.pay.cloud.KeyInfo.KEY_NUM_3;
import static com.pay.cloud.KeyInfo.KEY_NUM_4;
import static com.pay.cloud.KeyInfo.KEY_NUM_5;
import static com.pay.cloud.KeyInfo.KEY_NUM_6;
import static com.pay.cloud.KeyInfo.KEY_NUM_7;
import static com.pay.cloud.KeyInfo.KEY_NUM_8;
import static com.pay.cloud.KeyInfo.KEY_NUM_9;


public class PwdInputFragment extends Fragment implements KeyInfoListener {
    private static final List<KeyInfo> validKeys = new ArrayList<>();
    static {
        validKeys.add(KEY_NUM_0);
        validKeys.add(KEY_NUM_1);
        validKeys.add(KEY_NUM_2);
        validKeys.add(KEY_NUM_3);
        validKeys.add(KEY_NUM_4);
        validKeys.add(KEY_NUM_5);
        validKeys.add(KEY_NUM_6);
        validKeys.add(KEY_NUM_7);
        validKeys.add(KEY_NUM_8);
        validKeys.add(KEY_NUM_9);
    }

    private OnPwdListener listener;
    private String curPwd = "";
    private PwdInputItemFragment[] items = new PwdInputItemFragment[]{
            new PwdInputItemFragment(),
            new PwdInputItemFragment(),
            new PwdInputItemFragment(),
            new PwdInputItemFragment(),
            new PwdInputItemFragment(),
            new PwdInputItemFragment()
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pwd_input, container, false);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        for (PwdInputItemFragment item : items) {
            fragmentTransaction.add(R.id.refund_pwd_input_items_container, item);
        }
        fragmentTransaction.commit();
        return view;
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (keyInfo == KeyInfo.KEY_DELETE) {
            del();
        } else if (validKeys.contains(keyInfo)) {
            add(keyInfo.getValue());
        }
        return false;
    }

    public void setOnPwdListener(OnPwdListener listener) {
        this.listener = listener;
    }

    public interface OnPwdListener {
        void finish(String pwd);
    }

    private void add(String pwd) {
        int curLength = curPwd.length();
        if (curLength >= items.length) {
            return;
        }

        curPwd += pwd;
        curLength = curPwd.length();
        for (int i = 0; i < curLength; i++) {
            items[i].fill();
        }

        if (null != listener && curLength >= items.length) {
            listener.finish(curPwd);
        }
    }

    private void del() {
        int curLength = curPwd.length();
        if (curLength == 0) {
            return;
        }
        items[curLength - 1].clear();
        curPwd = curPwd.substring(0, curLength - 1);
    }

    public void clear() {
        curPwd = "";
        for (int i = 0; i < items.length; i++) {
            items[i].clear();
        }
    }

}
